package com.ca.ridersclubapplication.util;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.time.LocalDateTime;

public class TimeUtils {

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String parseShowDateTime(String dateTime) {

        if (dateTime == null) {
            return "";
        }

        LocalDateTime msgTime = LocalDateTime.parse(dateTime.replace(" ", "T"));
        LocalDateTime nowTime = LocalDateTime.now();


        int nowYear = nowTime.getYear();
        int msgYear = msgTime.getYear();
        if (nowYear == msgYear) {
            int nowDayOfYear = nowTime.getDayOfYear();
            int msgDayOfYear = msgTime.getDayOfYear();
            switch (nowDayOfYear - msgDayOfYear) {
                case 0:
                    return (msgTime.getHour() < 10 ? ("0" + msgTime.getHour()) : msgTime.getHour()) + ":" +
                            (msgTime.getMinute() < 10 ? ("0" + msgTime.getMinute()) : msgTime.getMinute());
                case 1:
                    return "昨天" +
                            (msgTime.getHour() < 10 ? ("0" + msgTime.getHour()) : msgTime.getHour()) + ":" +
                            (msgTime.getMinute() < 10 ? ("0" + msgTime.getMinute()) : msgTime.getMinute());
                case 2:
                    return "前天" +
                            (msgTime.getHour() < 10 ? ("0" + msgTime.getHour()) : msgTime.getHour()) + ":" +
                            (msgTime.getMinute() < 10 ? ("0" + msgTime.getMinute()) : msgTime.getMinute());
                default:
                    return (msgTime.getMonthValue() < 10 ? ("0" + msgTime.getMonthValue()) : msgTime.getMonthValue()) + "-" +
                            (msgTime.getDayOfMonth() < 10 ? ("0" + msgTime.getDayOfMonth()) : msgTime.getDayOfMonth()) + " " +
                            (msgTime.getHour() < 10 ? ("0" + msgTime.getHour()) : msgTime.getHour()) + ":" +
                            (msgTime.getMinute() < 10 ? ("0" + msgTime.getMinute()) : msgTime.getMinute());
            }
        }

        return msgTime.getYear() + "-" +
                (msgTime.getMonthValue() < 10 ? ("0" + msgTime.getMonthValue()) : msgTime.getMonthValue()) + "-" +
                (msgTime.getDayOfMonth() < 10 ? ("0" + msgTime.getDayOfMonth()) : msgTime.getDayOfMonth()) + " " +
                (msgTime.getHour() < 10 ? ("0" + msgTime.getHour()) : msgTime.getHour()) + ":" +
                (msgTime.getMinute() < 10 ? ("0" + msgTime.getMinute()) : msgTime.getMinute());
    }
}
